package com.songzhili.nohttpdemo.api;

import android.app.Activity;
import android.content.DialogInterface;

import com.songzhili.nohttpdemo.dialog.WaitDialog;
import com.yanzhenjie.nohttp.rest.OnResponseListener;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

/**
 * Created by Administrator on 2018/1/4 0004.
 */

public class HttpResponseListener<T> implements OnResponseListener<T> {
    private  HttpResponseCallBackListener<T> httpResponseCallBackListener;
    Activity mActivity;
    //是否显示请求框
    boolean isLoading;
    //是否允许点击弹出框其他地方关闭请求
    boolean canCancel;
    //是否接收失败回调
    boolean isShowonFailed;
    WaitDialog mWaitDialog;
    Request<?> mRequest;
    public HttpResponseListener(Request<?> request,HttpResponseCallBackListener<T> httpResponseCallBackListener, Activity activity, boolean isLoading, boolean canCancel) {
        this.httpResponseCallBackListener = httpResponseCallBackListener;
        this.mActivity = activity;
        this.isLoading = isLoading;
        this.canCancel = canCancel;
        this.mRequest=request;
        if (activity != null && isLoading) {
            mWaitDialog = new WaitDialog(mActivity);
            mWaitDialog.setCancelable(canCancel);
            mWaitDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    mRequest.cancel();
                }
            });
        }
    }
    //是否需要处理失败信息
    public HttpResponseListener(Request<?> request,HttpResponseCallBackListener<T> httpResponseCallBackListener, Activity activity, boolean isLoading, boolean canCancel,boolean isShowonFailed) {
        this.httpResponseCallBackListener = httpResponseCallBackListener;
        this.mActivity = activity;
        this.isLoading = isLoading;
        this.canCancel = canCancel;
        this.isShowonFailed=isShowonFailed;
        this.mRequest=request;
        if (activity != null && isLoading) {
            mWaitDialog = new WaitDialog(mActivity);
            mWaitDialog.setCancelable(canCancel);
            mWaitDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    mRequest.cancel();
                }
            });
        }
    }
    @Override
    public void onStart(int what) {
        if (mWaitDialog != null && !mActivity.isFinishing() && !mWaitDialog.isShowing())
            mWaitDialog.show();
    }

    @Override
    public void onSucceed(int what, Response<T> response) {
    if (response.responseCode()==200){
        T t = response.get();
        if (httpResponseCallBackListener!=null){
            httpResponseCallBackListener.onSucceed(what,response, t);
        }
    }
    }

    @Override
    public void onFailed(int what, Response response) {
        if (mWaitDialog != null && !mActivity.isFinishing() && mWaitDialog.isShowing())
            mWaitDialog.dismiss();
        if (isShowonFailed&&httpResponseCallBackListener!=null){
            httpResponseCallBackListener.onFailed(what, response);
        }
    }

    @Override
    public void onFinish(int what) {
        if (mWaitDialog != null && !mActivity.isFinishing() && mWaitDialog.isShowing())
            mWaitDialog.dismiss();
    }
}
